<template>
  <view class="container">
    <u--form
      labelPosition="left"
      labelWidth="90"
      :model="Source"
      :rules="rules"
      ref="uForm"
    >
      <u-form-item label="客户来源" prop="name" borderBottom>
        <u--input
          v-model="Source.name"
          border="none"
          placeholder="请输入客户来源"
        ></u--input>
      </u-form-item>
    </u--form>
    <view style="margin-top: 25px">
      <u-button type="warning" @click="submit">修改</u-button>
    </view>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
export default {
  data() {
    return {
      Source: {
        name: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请填客户来源",
            trigger: ["blur", "change"],
          },
        ],
      },
      id: 0,
    };
  },
  methods: {
    async getInfoData() {
      let data = {
        id: this.id,
      };

      let result = await this.$u.api.manage.SourceInfo(data);

      if (result.code === 0) {
        this.$refs.uToast.show({
          type: "error",
          message: result.msg,
          duration: 1500,
          complete: () => {
            this.$u.route("/pages/manage/source/index/index");
          },
        });
      }

      this.Source = result.data;
    },
    submit() {
      this.$refs.uForm
        .validate()
        .then(async () => {
          let result = await this.$u.api.manage.SourceEdit(this.Source);

          if (result.code === 1) {
            this.$refs.uToast.show({
              type: "success",
              message: result.msg,
              duration: 1500,
              complete: () => {
                this.$u.route({
                  type: "navigateBack",
                  delta: 1,
                });
              },
            });
          } else {
            this.$refs.uToast.show({
              type: "error",
              message: result.msg,
              duration: 1500,
            });
          }
        })
        .catch((error) => {});
    },
  },
  onReady() {
    //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    this.$refs.uForm.setRules(this.rules);
  },
  onLoad(option) {
    this.id = option.id ? option.id : 0;

    this.getInfoData();
  },
};
</script>

<style>
.container {
  height: 100%;
  margin: 0 50rpx;
  box-sizing: border-box;
}
</style>